Ext.namespace("Gfa");
Ext.namespace("Gfa.widgets");


Gfa.widgets.PagingToolbar = function(config){
	Gfa.widgets.PagingToolbar.superclass.constructor.call(this, config);
	if(config.showExportToExcel===undefined){
		if(ProjConfigParam!=undefined && ProjConfigParam.SHOW_EXPORT_TO_EXCEL)
			this.showExportToExcel = true ;
		else
			this.showExportToExcel = false ;
	}
	if(config.showExportAllToExcel===undefined){
		if(ProjConfigParam!=undefined && ProjConfigParam.SHOW_EXPORT_ALL_TO_EXCEL)
			this.showExportAllToExcel = true ;
		else
			this.showExportAllToExcel = false ;
	}
	if(config.allowSelectPageSize===undefined){
		if(ProjConfigParam!=undefined && ProjConfigParam.ALLOW_SELECT_PAGE_SIZE)
			this.allowSelectPageSize = true ;
		else
			this.allowSelectPageSize = false ;
	}
	/*if(config.showSortingAll===undefined)
		this.showSortingAll = true;*/
};

Ext.extend(Gfa.widgets.PagingToolbar, Ext.PagingToolbar, {
	comboPagesize :null ,
	showExportToExcel : true ,
	showExportAllToExcel : true ,

    afterRender : function(){
		Ext.PagingToolbar.superclass.afterRender.call(this);
		if (this.items && this.items.items) {
			for (var i = 0; i < this.items.items.length; i++) {
				var it = this.items.items[i];
				if (it instanceof Ext.form.ComboBox) {
					it.wrap.dom.style.width = '100px';
					if (it.trigger.dom.width < 2) {
						it.trigger.setWidth(17);
						it.trigger.setRight(0);
						it.trigger.setLeft(83);
						if (it.trigger.dom.width == 0) {
							it.trigger.setTop(-2);
						}
					}
				}
			}
		}
    },

   // private
    onRender : function(ct, position){
        Ext.PagingToolbar.superclass.onRender.call(this, ct, position);
		if(this.showPageTo!=false&&this.showPageTo!="undefied"){
			this.first = this.addButton( {
				tooltip : this.firstText,
				iconCls : "x-tbar-page-first",
				disabled : true,
				handler : this.onClick.createDelegate(this, [ "first" ])
			});
			this.prev = this.addButton( {
				tooltip : this.prevText,
				iconCls : "x-tbar-page-prev",
				disabled : true,
				handler : this.onClick.createDelegate(this, [ "prev" ])
			});
			this.addSeparator();
			this.add(this.beforePageText);
			this.field = Ext.get(this.addDom( {
				tag : "input",
				type : "text",
				size : "3",
				value : "1",
				cls : "x-tbar-page-number"
			}).el);
			this.field.on("keydown", this.onPagingKeydown, this);
			this.field.on("focus", function() {
				this.dom.select()
			});
			this.afterTextEl = this.addText(String.format(this.afterPageText, 1));
			this.field.setHeight(18);
			this.addSeparator();
			this.next = this.addButton( {
				tooltip : this.nextText,
				iconCls : "x-tbar-page-next",
				disabled : true,
				handler : this.onClick.createDelegate(this, [ "next" ])
			});
			this.last = this.addButton( {
				tooltip : this.lastText,
				iconCls : "x-tbar-page-last",
				disabled : true,
				handler : this.onClick.createDelegate(this, [ "last" ])
			});
			this.addSeparator();
			this.loading = this.addButton( {
				tooltip : this.refreshText,
				iconCls : "x-tbar-loading",
				hidden : true,
				handler : this.onClick.createDelegate(this, [ "refresh" ])
			});
		}else{
			this.first = this.addButton({});
			this.prev = this.addButton({});
			this.add("");
			this.field = Ext.get(this.addDom({}).el);
			this.afterTextEl = this.addText("");
			this.next = this.addButton({});
			this.last = this.addButton({});
			this.loading = this.addText("");
		}
        //this.addSeparator();
        if(this.allowSelectPageSize){
			var pageSizeListArr = this.grid.pageSizeList.split(',') ;
			var storeData = [] ;
			for(var i = 0;i<pageSizeListArr.length;i++){
				storeData[i] = [pageSizeListArr[i],parseInt(pageSizeListArr[i])] ;
			}

		    var store = new Ext.data.SimpleStore({
		        fields : ['disp','value'],
		        data : storeData
		    });
	          
			this.comboPagesize = new Ext.form.ComboBox({
				name : 'comboPagesize',	
				width:100,
				store: store,
				displayField:'disp'	,
				mode: 'local' , 
				//typeAhead: true, 
				forceSelection: true,
				editable: false,
				triggerAction: 'all',
				valueField:'value'	
			}) ;
			if(!this.pageSize){	
				 this.pageSize = this.grid.pageSizeList.split(',')[0] ;
				 //alert(this.grid.pageSizeList.split(',')[0]) ;
			}  
			
			this.comboPagesize.setValue(this.pageSize) ; 
			       
			this.addField(this.comboPagesize) ;
	        
			this.comboPagesize.on('select',function(){
	       		//alert(this.getPagesize());
	       		//this.onClick.createDelegate(this, ["first"]) ;
	       		this.pageSize = this.getPagesize() ;
	       		this.grid.pageSize = this.pageSize ;
	       		/*
	       		if(this.grid.pagingToolbar2){
	       			this.grid.pagingToolbar2.comboPagesize.setValue(this.pageSize);
	       		}
	       		*/
	       		this.grid.syncPagingToolbar(this) ;
	       		//this.grid.pagingToolbar2.fireEvent() ;
	       		
	       		
	//       		this.grid.regEvent() ;
	       		this.store.load();
	       	},this);
        }
        //添加下拉框:end

		//add export button
        if(this.showExportToExcel || this.showExportAllToExcel){
        	this.addSeparator();
        }
        
        if(this.grid instanceof Gfa.widgets.BaseGrid){
        	if(this.showExportToExcel){
	        	this.exportToexcel = this.addButton({
	                tooltip: i18n.basegrid.exportExcel,
	                iconCls: "wef-toolbar-excel",
	                //text:'excel' ,
	                handler: this.grid.exportToExcel.createDelegate(this.grid)
	            });
        	}
        	
        	if(this.showExportAllToExcel){
	            this.exportToexcel = this.addButton({
	                tooltip: i18n.basegrid.exportExcelAll,
	                iconCls: "wef-toolbar-excelAll",
	                //text:'excel all' ,
	                handler: this.grid.exportAllToExcel.createDelegate(this.grid)
	            });
        	}
        }
        //end:add export button

		//add Sorting config
        if(this.showSortingAll && this.grid instanceof Gfa.widgets.BaseGrid){
        	this.addSeparator();

			var storeSortProperyData = [];
			var j = 0;
			for (var i = 0; i < this.grid.colModel.config.length; i++) {
				var dataIndex = this.grid.colModel.getDataIndex(i);
				if (dataIndex != '' && dataIndex && dataIndex != this.grid.colModel.getColumnId(i)
						&& (this.grid.includeHidden || !this.grid.colModel.isHidden(i))) {
					storeSortProperyData[j] = [this.grid.colModel.getColumnHeader(i), dataIndex];
					j++;
				}
			}
			var storeSortPropery = new Ext.data.SimpleStore({
				fields : ['disp', 'value'],
				data : storeSortProperyData
			});
			this.comboSortPropery = new Ext.form.ComboBox({
				name : 'comboSortPropery',
				width : 100,
				store : storeSortPropery,
				displayField : 'disp',
				mode : 'local',
				forceSelection : true,
				editable : false,
				triggerAction : 'all',
				valueField : 'value'
			});
			this.addField(this.comboSortPropery);

        	this.sortingDir = this.addButton({
                tooltip : i18n.basegrid.sortingAll,
                iconCls : "",
                text : i18n.basegrid.sorting,
                menu : [{
                	id : 'masc',
                	cls : 'xg-hmenu-sort-asc',
                	text : i18n.basegrid.sortAsc,
                	handler : this.grid.sortingAll.createDelegate(this.grid, ['ASC'])
                }, {
                	id : 'mdesc',
                	cls : 'xg-hmenu-sort-desc',
                	text : i18n.basegrid.sortDesc,
                	handler : this.grid.sortingAll.createDelegate(this.grid, ['DESC'])
                }]
            });
        }
		//end:add Sorting config

        if(this.displayInfo){
            this.displayEl = Ext.fly(this.el.dom).createChild({cls:'x-paging-info'});
        }
        if(this.dsLoaded){
            this.onLoad.apply(this, this.dsLoaded);
        }
    } 
    
    , 
    
	getPagesize:function(){
		return this.comboPagesize.getValue() ;
	}
	/**/

}) ;

Ext.reg('pagingToolbar',Gfa.widgets.PagingToolbar);